<template>
  <div class="login">
    <div class="content">
      <div class="title">阳江农宝管理系统</div>
      <el-form ref="loginForm" :model="loginForm" :rules="rules" class="form">
        <el-form-item prop="accountName">
          <el-input
            v-model="loginForm.accountName"
            placeholder="请输入用户名/手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button class="block" type="primary" @click="login('loginForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from "@/api/users";
import utils from "@/utils";
import md5 from "md5";

export default {
  data() {
    return {
      loginForm: {
        accountName: "sysAdmin",
        password: "Root@123",
      },
      rules: {
        accountName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { returnObj } = await login({
            ...this.loginForm,
            password: md5(this.loginForm.password),
          });

          this.$store.dispatch("LOGIN", returnObj);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url("../assets/images/login-bg.png") no-repeat center center;
  background-size: 100% 100%;
  display: flex;
  flex-flow: column nowrap;
  justify-content: space-around;
  align-items: center;

  .content {
    width: 400px;
    height: 388px;
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px -1px 16px 0px rgba(0, 0, 0, 0.24);
    padding: 50px 30px;

    .title {
      color: #283241;
      font-size: 24px;
      font-weight: 500;
      text-align: center;
    }

    .form {
      margin-top: 50px;
    }
  }
}
</style>
